﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Mogubang_Sl.Core;

namespace Mogubang_Sl.Views
{
    public partial class MoguItem : UserControl
    {
        public MoguItem()
        {
            InitializeComponent();
        }

        internal void SetMoguUI(Entity.MoguData moguData)
        {
            string uri = "/Images/";

            switch (moguData.Type)
            {
                case Entity.MoguType.BlueMogu:
                    uri += "main_" + moguData.Level.ToString() + ".png";
                    break;
                case Entity.MoguType.NewLife:
                    uri += "item_life.png";
                    break;
                case Entity.MoguType.OrangeMogu:
                    uri += "orange_" + moguData.Level.ToString() + ".png";
                    break;
                case Entity.MoguType.ET:
                    uri += "et_" + moguData.Level.ToString() + ".png";
                    break;
            }

            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            imgItem.Source = bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="side">锁定来源的方向 0-left 1-right 2-top 3-bottom</param>
        internal void SetBubble(int side)
        {
            //Random r = new Random(DateTime.Now.Millisecond);

            //this.StoryboardBubble.RepeatBehavior = RepeatBehavior.Forever;
            //this.StoryboardBubble.BeginTime = TimeSpan.FromMilliseconds(r.Next(500));
            //this.StoryboardBubble.Begin();
            this.imageBubble.Visibility = System.Windows.Visibility.Visible;

            CompositeTransform tr = this.imageBubble.RenderTransform as CompositeTransform;
            switch (side)
            {
                case 0:
                    tr.Rotation = 0;
                    break;
                case 1:
                    tr.Rotation = 180;
                    break;
                case 2:
                    tr.Rotation = 90;
                    break;
                case 3:
                    tr.Rotation = -90;
                    break;
            }
        }

        internal void SetUnBubble()
        {
            //this.StoryboardBubble.Stop();
            this.imageBubble.Visibility = System.Windows.Visibility.Collapsed;
        }

        internal void Hide()
        {
            this.StoryboardHide.Begin();
        }

        internal void Show()
        {
            this.StoryboardShow.Begin();
        }

        internal void Show(int columnBlank)
        {
            this.Show();
            CompositeTransform trans = this.grid.RenderTransform as CompositeTransform;
            trans.TranslateY = GameStatics.ItemSize * columnBlank * -1;
            Duration duration = new Duration(TimeSpan.FromMilliseconds(columnBlank * GameStatics.baseDuration + 37));
            //掉下来
            DoubleAnimation translateY = new DoubleAnimation
            {
                To = 0,
                Duration = duration,
            };

            //弹跳
            QuadraticEase bounce = new QuadraticEase();
            bounce.EasingMode = EasingMode.EaseIn;
            //bounce.Bounces = 2;
            //bounce.Bounciness = 10;
            translateY.EasingFunction = bounce;

            Storyboard.SetTargetProperty(translateY, new PropertyPath("TranslateY"));

            Storyboard storyboard = new Storyboard { };
            Storyboard.SetTarget(storyboard, grid.RenderTransform);
            storyboard.Children.Add(translateY);
            storyboard.Begin();
        }

        internal void SetBombStatus()
        {
            this.regUseBombTool.Visibility = System.Windows.Visibility.Visible;
        }

        internal void SetWoodStatus()
        {
            this.regUseWoodTool.Visibility = System.Windows.Visibility.Visible;
        }

        internal void CancelToolStatus()
        {
            this.regUseBombTool.Visibility = System.Windows.Visibility.Collapsed;
            this.regUseWoodTool.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
